var data = require('data.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    luggage_list: data.luggage_list,
    show_add: "none",
    add_model: true,
    show_id: "",
    input_content: "",
    isChecked: false,
  },
  add_menu: function (e) {
    var show_id = e.currentTarget.dataset.id;
    this.setData({
      show_add: "display",
      show_id: show_id,
      add_model: false
    })
  },

  cancel_add: function () {
    this.setData({
      show_add: "none",
      add_model: true
    })
  },
  add: function (e) {
    console.log(e);
    var id = e.target.dataset.id;
    var luggage_list = this.data.luggage_list;
    var content = this.data.input_content;
    if(content != ''){
      var input_arr = {
        name: luggage_list[id]["content"].length,
        value: this.data.input_content,
        checked: false
      };

      luggage_list[id]["content"].push(input_arr);
      this.setData({
        show_add: "none",
        luggage_list: luggage_list,
        add_model: true,
        input_content: "",
      })
    }else{
      wx.showToast({
        title: '填写内容不能为空',
        icon: 'none',// 'none'为无图标
        duration: 2000 //多少毫秒后消失
      });
    }
  },
  input_content: function (e) {
    this.setData({
      input_content: e.detail.value
    })
  },
  check:function(e)
  {
    var that = this
    var id = e.currentTarget.dataset.id;
    var luggage = that.data.luggage_list;
    var items = that.data.luggage_list[id];
    var checkArr = e.currentTarget.dataset.itemId;
    luggage[id]["content"][checkArr]["checked"] = !luggage[id]["content"][checkArr]["checked"];
    console.log(luggage)
    this.setData({
      luggage_list: luggage
    })
  },
  checkChange: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id;
    var luggage = that.data.luggage_list;
    var items = that.data.luggage_list[id];
    var checkArr = e.currentTarget.dataset.itemId;
    luggage[id]["content"][checkArr]["checked"] = !luggage[id]["content"][checkArr]["checked"];
    this.setData({
      luggage_list:luggage
    })
    console.log(this.data.luggage_list)
  },
  delete_item:function(e){
    var that = this
    var id = e.currentTarget.dataset.id;
    var itemId = e.currentTarget.dataset.itemId;
    var luggage = that.data.luggage_list;
    var items = that.data.luggage_list[id];
    luggage[id]["content"].splice(itemId,1);
    this.setData({
      luggage_list: luggage
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    var that = this;
    wx.getStorage({
      key: 'luggage_list',
      success: function (res) {
        that.setData({
          luggage_list:res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var checked=new Array();
    var k = 0;
    var luggage_list = this.data.luggage_list;
    outer:
    for (var i = 0; i < luggage_list.length;i++)
    {
      var content = luggage_list[i]["content"];
      for(var j=0;j<content.length;j++)
      {
        checked[k] = content[j]["checked"];
        k++;
      }
    }
    
    var pages = getCurrentPages();
    var preprevPage = pages[pages.length - 2];
    var o=0;
    for(var l=0;l<checked.length;l++){
      if (checked[l]) {
        preprevPage.setData({
          id: 8,
          'item[8].status': false,
          'item[8].slide_width': 95,
        })
        // wx.navigateBack({
        //   delta: 1,
        // })
      }else{
        o++;
        if (o == checked.length)
        {
          preprevPage.setData({
            id: 8,
            'item[8].status': true,
          })
        }
      }
    }

    wx.setStorage({
      key: "luggage_list",
      data: this.data.luggage_list
    })
  },
})